package org.Image.management;

import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Service.StorageIo.Pojo.Mapper.FileStorage;
import org.Image.management.ViewCofig.file.IconPath;
import org.Image.management.ViewController.MainViewApp;
import org.Image.management.utils.FileDeletionService;
import org.Image.management.utils.JsonFileUtils;
import org.Image.management.utils.remind.ShowDialogImages;
import org.Image.management.utils.remind.ShowDialogUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.nio.file.Path;

import static javafx.application.Application.launch;

@SpringBootApplication
@Slf4j
public class SpringBootImageManagementApplication {

    public static ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) {
        // 启动图片窗口
        ShowDialogImages showDialogImages = ShowDialogUtils.DialogBuilder(202, 160, IconPath.StartSoftwareIcon.getIconURL());
        // 启动 SpringBoot
        applicationContext = SpringApplication.run(SpringBootImageManagementApplication.class, args);
        // 关闭图片窗口
        showDialogImages.CloseAnimationPrompt(longTime -> {
            log.info("SpringBoot 启动成功耗时：{}", longTime);
            // 启动 JavaFX
            launch(MainViewApp.class, args);

            System.gc();
            JsonFileUtils.readJsonFromFileToObject("deleteFile", FileStorage.class, true).forEach(e -> {
                if (e.getPath() != null) {
                    FileDeletionService.deleteFileWithRetryButThread(Path.of(e.getPath()), 5, 1000);
                }
            });
        });

    }

}
